//
//  VISTAMagazineVC.m
//  看杂志
//
//  Created by zcj on 15-12-14.
//  Copyright (c) 2015年 郑朝军. All rights reserved.
//

#import "VISTAMagazineVC.h"
#import "VISTAMagaCategoryVC.h"
#import "VISTASettingVC.h"
#import "VISTAMagaTableViewModel.h"
#import "MJRefresh.h"
#import "SelectFindHttp.h"
#import "Grouble.h"
#import "VISTAMagaTableViewCell.h"
#import "VISTAMagaDetailVC.h"

@interface VISTAMagazineVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)NSMutableArray *dataSourceArray;

@property(nonatomic, strong)UITableView *tableView;
@end

@implementation VISTAMagazineVC
- (NSMutableArray *)dataSourceArray
{
    if (_dataSourceArray == nil) {
        _dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self configViewAndNavi];
    [self setTableView];
    [self setRush];
    // Do any additional setup after loading the view.
}
- (void)setTableView
{
    //ui
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}


// 下拉刷新
- (void)setRush
{
    [self.tableView addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    NSMutableArray * idleImages = [NSMutableArray array];
    for (NSUInteger i = 1; i<=60; ++i) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%zd",i]];
        [idleImages addObject:image];
    }
    [self.tableView.gifHeader setImages:idleImages forState:MJRefreshHeaderStateIdle];
    //设置即将刷新状态的动画图片
    NSMutableArray *refreshingImages = [NSMutableArray array];
    for (NSInteger i = 1; i<=3; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_loading_0%zd",i]];
        [refreshingImages addObject:image];
    }
    [self.tableView.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStatePulling];
    
    //设置正在刷新是的动画图片
    [self.tableView.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStateRefreshing];
    
    //马上进入刷新状态
    [self.tableView.gifHeader beginRefreshing];
    
}

- (void)configViewAndNavi
{
    //导航栏背景
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NAVI"] forBarMetrics:UIBarMetricsDefault];
    //导航栏标题View
    UIImageView *titleImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"logo"]];
    self.navigationItem.titleView = titleImageView;
    //导航栏右按钮
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, 35, 35);
    [rightButton setImage:[UIImage imageNamed:@"设置_1"] forState:UIControlStateNormal];
    [rightButton setImage:[UIImage imageNamed:@"设置_2"] forState:UIControlStateHighlighted];
    [rightButton addTarget:self action:@selector(goToSettingVC) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightItem;
    //导航栏左按钮
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 35, 35);
    [leftButton setImage:[UIImage imageNamed:@"切换_1"] forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageNamed:@"切换_2"] forState:UIControlStateHighlighted];
    [leftButton addTarget:self action:@selector(exchange) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    //ViewController背景图片
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"资讯背景底"]];
}
- (void)goToSettingVC
{
    VISTASettingVC *settingVC = [[VISTASettingVC alloc]init];
    UINavigationController *setNavi = [[UINavigationController alloc]initWithRootViewController:settingVC];
    [self presentViewController:setNavi animated:YES completion:nil];
}
- (void)exchange
{
    VISTAMagaCategoryVC *magaCategoryVC = [[VISTAMagaCategoryVC alloc]init];
    [self.navigationController pushViewController:magaCategoryVC animated:YES];
}
// 下拉刷新
- (void)refreshData
{
     NSString *path = [NSString stringWithFormat:@"http://ktx.cms.palmtrends.com/api_v2.php?action=get_mags_list&sa=&uid=10067567&mobile=iphone5&offset=%ld&count=%ld&&e=40dab97d773e7860febfc897c04824e2&uid=10067567&pid=10053&mobile=iphone5&platform=i",(long)8,(long)15];
    
    ZCJLog(@"refreshData%@",path);
    
    [SelectFindHttp postWirhUrl:path parms:nil success:^(id json) {
        ZCJLog(@"%@",json);
        [self.dataSourceArray removeAllObjects];
        NSArray * list  = json[@"list"];
        for (NSDictionary * dict in list) {
            VISTAMagaTableViewModel * model = [VISTAMagaTableViewModel objectWithKeyValues:dict];
            model.ID = [dict objectForKey:@"id"];
            //model
            [self.dataSourceArray addObject:model];
        }
        [self.tableView reloadData];
        
        [self.tableView.gifHeader endRefreshing];
        [MBProgressHUD showSuccess:@"加载成功"];
    } failture:^(id error) {
        ZCJLog(@"%@",error);
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    VISTAMagaTableViewCell *cell = [VISTAMagaTableViewCell cellWithTableView:tableView];
  
    [cell fillCellWithModel:self.dataSourceArray[indexPath.row]];
    return cell;
   
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    VISTAMagaDetailVC *detailVC = [[VISTAMagaDetailVC alloc]init];
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:detailVC];
    
    VISTAMagaTableViewModel *model =self.dataSourceArray[indexPath.row];
    detailVC.textId = model.ID;
    detailVC.vol_year = model.vol_year;
 
    [self presentViewController:navi animated:NO completion:nil];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
